//
//  BusStopViewController.m
//  Bus_iPhone
//
//  Created by antunnel on 12-7-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "BusStopViewController.h"
#import "BusStopCell.h"
#import "BusStopDetailViewController.h"

@interface BusStopViewController ()

@end

@implementation BusStopViewController


-(void)dealloc{

    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"ShowBusStopDetailNotication" object:nil];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showStopDetail:) name:@"ShowBusStopDetailNotication" object:nil];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

	// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

//

-(void)fuzzySearchWithWord:(NSString *)text{

    self.list=[[BusDataManager currentBusDataManager]fuzzyQueryBusStopListWithFuzzyWord:text];
}

-(void)showPassStopBusLineListWithStopDic:(NSDictionary *)dic{

    UINavigationController *tempNavigationController=self.navigationController;
    self.tabBarController.selectedViewController=tempNavigationController;
    
    NSArray *array=[[BusDataManager currentBusDataManager]queryBusLineListPassTheStopID:[dic objectForKey:@"stopID"]];
    
    BusStopDetailViewController *stop=[[BusStopDetailViewController alloc]initWithNibName:@"BusStopDetailViewController" bundle:nil];
    NSString *name=[dic objectForKey:@"stopName"];
    stop.title=name;
    stop.lineList=array;
    stop.stopName=name;
    
    NSArray *viewControllers=[NSArray arrayWithObjects:self,stop, nil];
    [self.navigationController setViewControllers:viewControllers animated:YES];
    [stop release];
}

-(void)showStopDetail:(NSNotification *)not{

    NSDictionary *dic=[not object];
    [self showPassStopBusLineListWithStopDic:dic];
}

#pragma mark -
#pragma mark -UITableViewDataSource

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 50.0f;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellString=@"bus stop";
    
    BusStopCell *cell=[tableView dequeueReusableCellWithIdentifier:cellString];
    if (!cell) {
        cell=[[[BusStopCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellString] autorelease];
        cell.backgroundColor=[UIColor clearColor];
    }
    
    NSDictionary *dic=[self.list objectAtIndex:indexPath.row];
    cell.busImgView.image=[UIImage imageNamed:@"busStopClick.png"];
    cell.stopNameLabel.text=[dic objectForKey:@"stopName"];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    
    NSDictionary *dic=[self.list objectAtIndex:indexPath.row];
    [self showPassStopBusLineListWithStopDic:dic];
}
@end
